package com.cn.study.access;

import com.cn.study.common.ResultCode;
import com.cn.study.entity.ResultVO;
import com.cn.study.tool.SignUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;

/**
 * @author mjy
 * @date 2021/12/29
 * @description
 */
public class WebSignature extends Signature {

    /**
     * 前端加密签名(加密由key+token+time组成)
     */
    private String headerSing = "headerSing";

    private String accessToken = "accessToken";

    private String headerTime = "headerTime";


    private String key = "mjy";

    @Override
    public ResultVO verifySignature(ServerHttpRequest request) {
        String sing = request.getHeaders().getFirst(headerSing);
        String token = request.getHeaders().getFirst(accessToken);
        String time = request.getHeaders().getFirst(headerTime);
        if (StringUtils.isBlank(sing) || StringUtils.isBlank(token) || StringUtils.isBlank(time)) {
            return ResultVO.fail(ResultCode.ENCRYPT_VERIFY);
        }
        if (sing.equals(SignUtil.md5(key + accessToken + time))) {
            return null;
        }
        return ResultVO.fail(ResultCode.ENCRYPT_VERIFY);
    }
}
